package com.heima.common.tess4j;

import lombok.Getter;
import lombok.Setter;
import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.awt.image.BufferedImage;
import java.io.File;

@Getter
@Setter
@Component
@ConfigurationProperties(prefix = "tess4j")
public class Tess4jClient {

    private String dataPath;
    private String language;

    /**
     * 识别图片中的文字
     * @param image
     */
    public String  doOCR(BufferedImage image) throws TesseractException {

        // 创建tesseract实体
        ITesseract tesseract = new Tesseract();
        // 设置字体库路径
        tesseract.setDatapath(dataPath);
        // 设置语言--简体中文
        tesseract.setLanguage(language);
        // 执行OCR识别
        String result = tesseract.doOCR(image);
        // 替换回车键Tab键，是结果为一行,将空格清除
        result = result.replaceAll("\n|\r","-").replaceAll(" ","");
        return  result;
    }
}
